package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.*;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.*;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.ItemService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.SelectItemListVo;
import com.rm.zeyou.vo.request.SelectUpgradeItemListVo;
import com.rm.zeyou.vo.response.SelectItemDetailResponseVo;
import com.rm.zeyou.vo.response.SelectUserCollectionListResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务项目表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private UserMemberMapper userMemberMapper;
    @Resource
    private UserCollectionMapper userCollectionMapper;
    @Resource
    private ItemSpecsMapper itemSpecsMapper;
    @Resource
    private UserItemOrderMapper userItemOrderMapper;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;


    @Override
    public ApiRestResponse<PageInfo<SelectUserCollectionListResponseVo>> selectItemList(SelectItemListVo selectItemListVo) {
        return ApiRestResponse.success(PageHelper.startPage(selectItemListVo.getPageNum(), selectItemListVo.getPageSize())
                .doSelectPageInfo(() -> itemMapper.selectItemList()));
    }

    @Override
    public ApiRestResponse<SelectItemDetailResponseVo> selectItemDetail(Long userId, SelectItemListVo selectVo) {
        Item item = itemMapper.selectById(selectVo.getItemId());
        if (CheckEmptyUtil.isEmpty(item)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.ITEM_EMPTY);
        }
        SelectItemDetailResponseVo vo = new SelectItemDetailResponseVo();
        vo.setItemName(item.getItemName());
        vo.setItemBanner(item.getItemBanner());
        vo.setItemTime(item.getItemTime());
        vo.setItemIntroduction(item.getItemIntroduction());
        vo.setItemFeatures(item.getItemFeatures());
        vo.setItemDescription(item.getItemDescription());
        vo.setItemOrdersSold(item.getRealSalesCount() + item.getVirtualSalesCount());
        vo.setItemPrice(item.getItemPrice());
        vo.setItemMemberPrice(item.getItemMemberPrice());
        int isVip = userMemberMapper.selectCount(new LambdaQueryWrapper<UserMember>()
                .eq(UserMember::getUserId, userId)).intValue();
        vo.setMemberStatus(isVip);
        int isCollect = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getItemId, selectVo.getItemId())).intValue();
        vo.setCollectionStatus(isCollect);


        vo.setDiscountPrice(item.getItemPrice().subtract(item.getItemMemberPrice()));
        vo.setItemSpecsList(itemSpecsMapper.selectList(new LambdaQueryWrapper<ItemSpecs>()
                .eq(ItemSpecs::getItemId, selectVo.getItemId())));

        return ApiRestResponse.success(vo);
    }

    @Override
    public ApiRestResponse<PageInfo<SelectUserCollectionListResponseVo>> selectUpgradeItemList(Long tid, SelectUpgradeItemListVo upgradeItemListVo) {
        UserItemOrder userItemOrder = userItemOrderMapper.selectOneByOrderNo(upgradeItemListVo.getOrderNo());
        //UserItemOrder userItemOrder = userItemOrderMapper.selectOne(new LambdaQueryWrapper<UserItemOrder>()
        //        .eq(UserItemOrder::getOrderNo, upgradeItemListVo.getOrderNo()));
        Item item = itemMapper.selectById(userItemOrder.getItemId());
        return ApiRestResponse.success( PageHelper.startPage(upgradeItemListVo.getPageNum(),upgradeItemListVo.getPageSize())
                .doSelectPageInfo(() ->  itemMapper.selectUpgradeItemList(userItemOrder.getTid(), item.getItemPrice())));
    }

}
